home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / admin / logins < prev    next >
Encoding:
Korn shell script  |  1997-08-26  |  6.7 KB  |  237 lines

  1. #!/bin/ksh
  2. # @(#) logins.ksh 1.4 96/03/31
  3. # 92/08/20 john h. dubois iii (john@armory.com)
  4. # 93/01/23 added printing of status if no args
  5. # 93/12/08 Create new login, not new issue
  6. # 94/02/18 Don't try to kill gettys if there are none.
  7. # 94/05/21 Added q option.
  8. # 94/05/24 Allow logins on console even when logins are off.
  9. # 94/09/25 Use mv -f to avoid query when moving a file being executed.
  10. # 94/10/22 Make console (sc_m) gettydefs entry special.
  11. #          Note: sc_m might also be used by some multi-console adapters?
  12. # 94/11/06 Added 'reason'
  13. # 94/11/29 Added /etc/default/logins
  14. # 94/12/15 Added syscon and console to built-in list of console ttys,
  15. #          in case those names are linked to e.g. tty01
  16. # 95/04/17 Use uname -n instead of just uname
  17. # 96/02/06 Set noglob in new /bin/login so that patterns can be used in
  18. #          /etc/default/logins.
  19. # 96/03/31 Replace *!login instead of *@!login because some systems have
  20. #          system name hard coded.  Match sc_m anywhere on line instead of
  21. #          only at the beginning, so that console line will hopefully be
  22. #          recognized by the 'next tag' field if the prompt is on a
  23. #          different line than the initial tag field.
  24.  
  25. function qprint {
  26.     $quiet || print -- "$lname: $*"
  27. }
  28.  
  29. function kill_gettys {
  30.     typeset PIDs
  31.  
  32.     PIDs=$(ps -e | awk '{ if ($NF == "getty") print $1; }')
  33.     if [ -n "$PIDs" ]; then
  34.     kill $PIDs
  35.     qprint "gettys killed."
  36.     else
  37.     print -u2 "No gettys to kill."
  38.     fi
  39. }
  40.  
  41. # Usage: disable_logins [reason]
  42. function disable_logins {
  43.     # Create new files
  44.     typeset CMessage="[non-console logins disabled] @!login: "
  45.     # Must use uname instead of @ here because $Message is also put in
  46.     # replacement login script
  47.     typeset Message="$(uname -n) is not allowing logins at this time."
  48.     [ -n "$1" ] && Message="$Message  Reason: $1"
  49.  
  50.     sed "
  51. /sc_m/ {
  52.     s/[^#]*!login:[^#]*/$CMessage/
  53.     p
  54.     d
  55. }
  56. s/[^#]*!login:[^#]*/$Message/" \
  57.     $defs > $defs+
  58.     chmod a+r $defs+
  59.     chgrp sys $defs+
  60.     qprint "Created new $defs"
  61.     qprint "Non-console login prompt:\n$Message"
  62.  
  63.     # Always allow logins on console; also allow logins on ttys named in
  64.     # 
  65.     echo \
  66. '#!/bin/ksh
  67. tty=$(/bin/tty)
  68. tty=${tty#/dev/}
  69. set -o noglob
  70. if [[ "$tty" = tty@([01][0-9]|syscon|console) ]] || { 
  71. sed -n "/^OVERRIDE_TTY=/{s///;p;}" /etc/default/logins 2>/dev/null |
  72. read ttypat && eval [[ \"$tty\" = $ttypat ]]; } then
  73.     exec /bin/login.b "$@"
  74. else
  75.     echo '"$Message"'
  76. fi' > $login+
  77.     chmod a+rx $login+
  78.     qprint "Created new $login"
  79.  
  80.     files="$defs $login"
  81.     # Save files by linking them to file.b
  82.     for file in $files; do
  83.     if [ -f $file.b ]; then
  84.         if [[ ! $file -ef $file.b ]]; then
  85.         echo "$lname: $file and $file.b are different.  Aborting."
  86.         exit 1
  87.         else
  88.         qprint "$file was already linked to $file.b"
  89.         fi
  90.     else
  91.         if ln $file $file.b; then
  92.         qprint "Saved old $file to $file.b"
  93.         else
  94.         echo "$lname: Could not link $file to $file.b.  Aborting."
  95.         exit 1
  96.         fi
  97.     fi
  98.     done
  99.     # If saves succeeded, move in new file
  100.     for file in $files; do
  101.     mv -f $file+ $file
  102.     qprint "Replaced $file with new version."
  103.     done
  104.  
  105.     # Don't want a welcome message; move it if we have one
  106.     if [ -f $issue -a ! -f $issue.b ]; then
  107.     mv -f $issue $issue.b
  108.     qprint "Moved aside $issue"
  109.     fi
  110.  
  111.     kill_gettys
  112.  
  113.     echo "$lname: Logins are disabled."
  114.     return 0
  115. }
  116.  
  117. function enable_logins {
  118.     # Files that are moved aside if they exist when disabling logins
  119.     # Existance is is an error, since they should have been moved;
  120.     # nonexistance of backup isn't.
  121.     for file in $issue; do
  122.     if [ -f $file ]; then
  123.         [ "$1" != boot ] && echo "$lname: $file already existed."
  124.     elif [ -f $file.b ]; then
  125.         mv -f $file.b $file
  126.         qprint "Moved $file.b to $file"
  127.     else
  128.         qprint "Note: no $file"
  129.     fi
  130.     done
  131.  
  132.     # Files that are moved aside & replaced with sustitutes when disabling
  133.     for file in $login $defs; do
  134.     if [ -f $file.b ]; then
  135.         mv -f $file.b $file
  136.         qprint "Moved $file.b to $file"
  137.     else
  138.         [ "$1" != boot ] && echo "$lname: Couldn't find $file.b."
  139.     fi
  140.     done
  141.     [ "$1" != boot ] && kill_gettys
  142.  
  143.     echo "$lname: Logins are enabled."
  144. }
  145.  
  146. lname=${0##*/}
  147. Usage="Usage: $lname [-hq] [on|off [reason]|boot]"
  148. defs=/etc/gettydefs
  149. issue=/etc/issue
  150. login=/bin/login
  151. quiet=false
  152.  
  153. while getopts :hq opt; do
  154.     case $opt in
  155.     h)
  156.     echo \
  157. "$lname: Enable or disable system logins.
  158. $Usage
  159. $lname off [reason]   disables logins by moving /bin/login.  It is replaced
  160.         with a script that only allows logins on the console, and on
  161.         ttys that matches the pattern given by the ksh pattern assigned to
  162.         OVERRIDE_TTY in /etc/default/logins.  An example line would be
  163.         OVERRIDE_TTY=tty[12]a
  164.         A new /etc/gettydefs file is created with prompts informing users
  165.         that logins are currently disallowed.  The prompt in the sc_m
  166.         gettydefs entry, usually used only by console gettys, is changed to
  167.         a prompt reminding the console user that non-console logins are
  168.         disabled.  If any arguments are given after \"off\", they are
  169.         included in the replacement login prompt as the reason that logins
  170.         have been disabled.  If not, no reason is included.  All gettys are
  171.         killed so that the new prompts will be read.  /etc/issue is moved
  172.         so that if other processes (telnetd, rlogind, etc.) try to read it
  173.         it will not be printed.
  174. $lname on   enables logins by undoing the above changes.  gettys are killed.
  175. $lname boot is identical to 'logins on' except that gettys are not killed
  176.             and no complaints are issued if logins were not off.  This would
  177.         typically be put in one of the /etc/rc2.d scripts.
  178. With no argument, $lname prints the login status.
  179. Options:
  180. -h: Print this help.
  181. -q: Quiet operation."
  182.     exit 0
  183.     ;;
  184.     q)
  185.     quiet=true
  186.     ;;
  187.     +?)
  188.     print -u2 "$lname: options should not be preceded by a '+'."
  189.     exit 1
  190.     ;;
  191.     :)
  192.         print -r -u2 -- \
  193.         "$lname: Option '$OPTARG' requires a value.  Use -h for help."
  194.         exit 1
  195.         ;;
  196.     ?) 
  197.     print -u2 "$lname: $OPTARG: bad option.  Use -h for help."
  198.     exit 1
  199.     ;;
  200.     esac
  201. done
  202.  
  203. # remove args that were options
  204. let OPTIND=OPTIND-1
  205. shift $OPTIND
  206.  
  207. set -e
  208.  
  209. if [ $# -eq 0 ]; then
  210.     if [ ! -f $defs.b -a ! -f $login.b ]; then
  211.     echo "logins are enabled."
  212.     elif [ -f $defs.b -a -f $login.b ]; then
  213.     echo "logins are disabled."
  214.     else
  215.     echo "logins are in a mixed state."
  216.     fi
  217. else
  218.     State=$1
  219.     shift
  220.     if [ $# -gt 0 ]; then
  221.     if [ "$State" = off ]; then
  222.         reason="$*"
  223.     else
  224.         print -u2 "$Usage\nUse -h for help."
  225.         exit 1
  226.     fi
  227.     fi
  228.     case "$State" in
  229.     boot) quiet=true
  230.       enable_logins boot;;
  231.     on)      enable_logins on;;
  232.     off)  disable_logins "$reason";;
  233.     *)    echo "$lname: Unrecognized command \"$State\".  Use -h for help."
  234.       exit 1;;
  235.     esac
  236. fi
  237.